package com.jcvision.mms.util;

import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.CharBuffer;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.ObjectMapper;

public class MobileRegion {

	private static final String QUERY_URL = "http://api.showji.com/Locating/www.showji.com.aspx?output=json&m=";
	
	public Map<String, String> search(String mobileNumber) {
		if(mobileNumber != null){
			
			String url = QUERY_URL + mobileNumber.trim();
			HttpGet httpGet = new HttpGet(url);
			DefaultHttpClient httpclient = new DefaultHttpClient();
			
			try {
				HttpResponse response = httpclient.execute(httpGet);
				HttpEntity entity = response.getEntity();
				InputStreamReader reader = new InputStreamReader(entity.getContent());
				CharBuffer cb = CharBuffer.allocate(1024);
				StringBuilder xmlBuilder = new StringBuilder();
				for(int count = reader.read(cb); count > -1; count = reader.read(cb)){
					xmlBuilder.append(cb.array(), 0, cb.position());
					cb.clear();
				}
				reader.close();
				EntityUtils.consume(entity);
				
				return jsonToMap(xmlBuilder.toString());
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				httpGet.releaseConnection();
			}
			
		}
		return null;
	}
	
	private Map<String, String> jsonToMap(String json){
		ObjectMapper mapper = new ObjectMapper();
		Map<String, String> result = new HashMap<>();
		try {
			JsonNode jsonNode = mapper.readTree(json);
			Iterator<Entry<String, JsonNode>> iter = jsonNode.getFields();
			while (iter.hasNext()) {
				Entry<String, JsonNode> entry = iter.next();
				result.put(entry.getKey(), entry.getValue().asText());
			}
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	public static void main(String[] args) throws IOException, HttpException, InterruptedException {
		
		/*/
		String url = "http://192.168.0.251:8080/mobileradiocms/activityclient/queryRadioSort.do";
		HttpGet httpGet = new HttpGet(url);
		DefaultHttpClient httpclient = new DefaultHttpClient();
		HttpResponse response = httpclient.execute(httpGet);
		HttpEntity entity = response.getEntity();
		InputStreamReader reader = new InputStreamReader(entity.getContent());
		StringBuilder sb = new StringBuilder();
		CharBuffer cb = CharBuffer.allocate(1024);
		
		for(int count = reader.read(cb); count > -1; count = reader.read(cb)){
			sb.append(cb.array(), 0, cb.position());
			cb.clear();
		}
		
		reader.close();
		EntityUtils.consume(entity);
		httpGet.releaseConnection();
		
		ObjectMapper mapper = new ObjectMapper();
		mapper.readTree(sb.toString());
		System.out.println(mapper);
		//*/
		
	}

}
